package com.controller;/**
 * @program: SmbmsDemo
 * @description: 订单控制器
 * @author: Mr.Kang
 * @create: 2020-06-01 10:42
 **/

import com.alibaba.fastjson.JSONArray;
import com.mysql.cj.util.StringUtils;
import com.pojo.Bill;
import com.pojo.Provider;
import com.pojo.User;
import com.service.BillService;
import com.service.ProviderService;
import com.service.ProviderServiceImpl;
import com.tools.Constants;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @Description:
 * @author 康康真滴暖
 * @date 2020-06-01 10:42
 */

@Controller
public class BillController {
    private Logger log= LogManager.getLogger(BillController.class);

    @Resource
    private BillService billService;
    @Resource
    private ProviderService providerService;

    @RequestMapping(value = "addBill.do")
    public String addBill(){
        return "billadd";
    }

    @RequestMapping(value = "modifyBill.do")
    public String modifyBill(){
        return "billmodify";
    }

   @RequestMapping(value = "getProviderlist")
   @ResponseBody
    public String getProviderlist(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        List<Provider> providerList = new ArrayList<Provider>();
        providerList = providerService.getProviderList(null,null);
        String a=JSONArray.toJSONString(providerList);
        return a;
    }



    @RequestMapping(value = "query")
    public String query(HttpServletRequest request, HttpServletResponse response)
            throws Exception{

        List<Provider> providerList = new ArrayList<Provider>();
        providerList = providerService.getProviderList("","");
        request.setAttribute("providerList", providerList);

        String queryProductName = request.getParameter("queryProductName");
        String queryProviderId = request.getParameter("queryProviderId");
        String queryIsPayment = request.getParameter("queryIsPayment");
        if(StringUtils.isNullOrEmpty(queryProductName)){
            queryProductName = "";
        }
        List<Bill> billList = new ArrayList<Bill>();
        Bill bill = new Bill();
        if(StringUtils.isNullOrEmpty(queryIsPayment)){
            bill.setIsPayment(0);
        }else{
            bill.setIsPayment(Integer.parseInt(queryIsPayment));
        }

        if(StringUtils.isNullOrEmpty(queryProviderId)){
            bill.setProviderId(0);
        }else{
            bill.setProviderId(Integer.parseInt(queryProviderId));
        }
        bill.setProductName(queryProductName);
        billList = billService.getBillList(bill);
        request.setAttribute("billList", billList);
        request.setAttribute("queryProductName", queryProductName);
        request.setAttribute("queryProviderId", queryProviderId);
        request.setAttribute("queryIsPayment", queryIsPayment);

       return "billlist";
    }

    @RequestMapping(value = "getBillById")
    public String getBillById(HttpServletRequest request, HttpServletResponse response,String url)
            throws Exception {
        String id = request.getParameter("billid");
        if(!StringUtils.isNullOrEmpty(id)){
            /*  BillService billService = new BillServiceImpl()*/;
            Bill bill = null;
            bill = billService.getBillById(id);
            request.setAttribute("bill", bill);
            return url;
        }
        return url;
    }

    @RequestMapping(value = "modify")
    public String modify(HttpServletRequest request, HttpServletResponse response)throws Exception {
        System.out.println("modify===============");
        String id = request.getParameter("id");
        String productName = request.getParameter("productName");
        String productDesc = request.getParameter("productDesc");
        String productUnit = request.getParameter("productUnit");
        String productCount = request.getParameter("productCount");
        String totalPrice = request.getParameter("totalPrice");
        String providerId = request.getParameter("providerId");
        String isPayment = request.getParameter("isPayment");

        Bill bill = new Bill();
        bill.setId(Integer.valueOf(id));
        bill.setProductName(productName);
        bill.setProductDesc(productDesc);
        bill.setProductUnit(productUnit);
        bill.setProductCount(new BigDecimal(productCount).setScale(2, BigDecimal.ROUND_DOWN));
        bill.setIsPayment(Integer.parseInt(isPayment));
        bill.setTotalPrice(new BigDecimal(totalPrice).setScale(2, BigDecimal.ROUND_DOWN));
        bill.setProviderId(Integer.parseInt(providerId));

        bill.setModifyBy(((User) request.getSession().getAttribute(Constants.USER_SESSION)).getId());
        bill.setModifyDate(new Date());
        int count = billService.modify(bill);
        if (count>0) {
            return "redirect:/query";
        } else {
           return "billmodify";
        }
    }

    @RequestMapping(value = "delBill")
    @ResponseBody
    public String delBill(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String id = request.getParameter("billid");
        HashMap<String, String> resultMap = new HashMap<String, String>();
        if(!StringUtils.isNullOrEmpty(id)){
          int count = billService.deleteBillById(id);
            if(count>0){//删除成功
                resultMap.put("delResult", "true");
            }else{//删除失败
                resultMap.put("delResult", "false");
            }
        }else{
            resultMap.put("delResult", "notexit");
        }
        String a=JSONArray.toJSONString(resultMap);
        return a;

    }
    @RequestMapping(value = "add")
    public String add(HttpServletRequest request, HttpServletResponse response)throws Exception{
        String billCode = request.getParameter("billCode");
        String productName = request.getParameter("productName");
        String productDesc = request.getParameter("productDesc");
        String productUnit = request.getParameter("productUnit");

        String productCount = request.getParameter("productCount");
        String totalPrice = request.getParameter("totalPrice");
        String providerId = request.getParameter("providerId");
        String isPayment = request.getParameter("isPayment");

        Bill bill = new Bill();
        bill.setBillCode(billCode);
        bill.setProductName(productName);
        bill.setProductDesc(productDesc);
        bill.setProductUnit(productUnit);
        bill.setProductCount(new BigDecimal(productCount).setScale(2,BigDecimal.ROUND_DOWN));
        bill.setIsPayment(Integer.parseInt(isPayment));
        bill.setTotalPrice(new BigDecimal(totalPrice).setScale(2,BigDecimal.ROUND_DOWN));
        bill.setProviderId(Integer.parseInt(providerId));
        bill.setCreatedBy(((User)request.getSession().getAttribute(Constants.USER_SESSION)).getId());
        bill.setCreationDate(new Date());
        int count = billService.add(bill);
        System.out.println("add flag -- > " + count);
        if(count>0){
            return "redirect:/query";
        }else{
            return "billadd";
        }
    }
}
